package org.JWCB.friend.aspect;


import org.JWCB.common.core.constants.UserConstants;
import org.JWCB.common.core.enums.ResultCode;
import org.JWCB.common.core.enums.UserStatus;
import org.JWCB.common.core.exception.ServiceException;
import org.JWCB.common.core.utils.ThreadLocalUtil;
import org.JWCB.friend.domain.user.vo.UserDetailVo;
import org.JWCB.friend.manager.UserCacheManager;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UserCheckStatusAspect {
    @Autowired
    private UserCacheManager userCacheManager;

    @Before(value = "@annotation(org.JWCB.friend.aspect.CheckUserStatus)")
    public void before(JoinPoint point) {
        Long userId = ThreadLocalUtil.get(UserConstants.USER_ID,Long.class);
        UserDetailVo userInfo = userCacheManager.getUserInfo(userId);
        if(userInfo == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        if(UserStatus.BLACKED.getCode().equals(userInfo.getStatus())) {
            // 被拉黑
            throw new ServiceException(ResultCode.FAILED_USER_BANNED);
        }
    }
}
